package crawler.web.engine.config;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.BitSet;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import library.Tracer;
import library.functions.Function2PV;

import org.apache.commons.lang.StringUtils;

import crawler.web.beans.CrawlURL;
import crawler.web.beans.ProxySetting;

/**
 * @author waynechen Mar 12, 2012 11:23:14 AM
 */
public class ConfigBatchInit {

	public static LinkedList<ProxySetting> getProxyList() {
		LinkedList<ProxySetting> list = new LinkedList<ProxySetting>();
		String path = ClassLoader.getSystemResource("proxyList").getPath();
		getList(path, list, new Function2PV<List<?>, String>() {
			@SuppressWarnings("unchecked")
			@Override
			public void apply(List<?> a, String line) {
				List<ProxySetting> aa = (List<ProxySetting>) a;
				String[] split = line.split(":");
				aa.add(new ProxySetting(split[0], Integer.parseInt(split[1])));
			}
		});
		Collections.shuffle(list);
		return list;
	}

	public static LinkedList<CrawlURL> getCrawlURLList() {
		LinkedList<CrawlURL> list = new LinkedList<CrawlURL>();
		String path = ClassLoader.getSystemResource("urlList").getPath();
		getList(path, list, new Function2PV<List<?>, String>() {
			@SuppressWarnings("unchecked")
			@Override
			public void apply(List<?> a, String line) {
				List<CrawlURL> aa = (List<CrawlURL>) a;
				aa.add(new CrawlURL(line));
			}
		});
		return list;
	}

	public static List<String> getStringList(String confFileName) {
		List<String> list = new LinkedList<String>();
		String path = ClassLoader.getSystemResource(confFileName).getPath();
		getList(path, list, new Function2PV<List<?>, String>() {
			@SuppressWarnings("unchecked")
			@Override
			public void apply(List<?> a, String line) {
				List<String> aa = (List<String>) a;
				aa.add(line);
			}
		});
		return list;
	}

	private static void getList(String path, List<?> list, Function2PV<List<?>, String> func) {
		File f = new File(path);
		if (!f.exists())
			return;
		try {
			FileReader in = new FileReader(f);
			BufferedReader reader = new BufferedReader(in);
			String line;
			while (null != (line = reader.readLine())) {
				line = line.trim();
				if (StringUtils.isEmpty(line))
					continue;
				//注释掉的就跳过
				else if (line.startsWith("#"))
					continue;
				else {
					if (line.indexOf("#") != -1)
						line = line.substring(0, line.indexOf("#"));
					line = line.trim();
					func.apply(list, line);
				}
			}
		} catch (FileNotFoundException e) {
			Tracer.exception(e);
		} catch (IOException e) {
			Tracer.exception(e);
		}
	}

	//---------------------bit set---------------------------

	public static BitSet loadBitSet(File file) {
		if (file == null || !file.exists())
			return null;
		ObjectInputStream in = null;
		BitSet bitSet = null;
		try {
			in = new ObjectInputStream(new FileInputStream(file));
			bitSet = (BitSet) in.readObject();
		} catch (Exception e) {
			Tracer.exception(e);
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
					Tracer.exception(e);
				}
		}
		return bitSet;
	}

	public static void saveBitSet(BitSet bits, File file) {
		if (bits == null || file == null)
			return;
		if (file.exists())
			file.delete();
		ObjectOutputStream out = null;
		try {
			out = new ObjectOutputStream(new FileOutputStream(file));
			out.writeObject(bits);
		} catch (IOException e) {
			Tracer.exception(e);
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (IOException e) {
					Tracer.exception(e);
				}
		}
	}
}

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//